import http from '@/http';
import type { 
  Workflow, 
  WorkflowQuery, 
  WorkflowForm, 
  WorkflowInstance, 
  WorkflowGraphData,
  WorkflowTrace,
  PageResult 
} from './WorkflowModel';

// 获取工作流列表（分页）
export const getWorkflowList = (params: WorkflowQuery) => {
  return http.post<PageResult<Workflow>>('/api/workflow/list', params);
};

// 添加工作流
export const addWorkflow = (data: WorkflowForm) => {
  return http.post<boolean>('/api/workflow/add', data);
};

// 更新工作流
export const updateWorkflow = (data: WorkflowForm) => {
  return http.put<boolean>('/api/workflow/update', data);
};

// 删除工作流
export const deleteWorkflow = (id: number) => {
  return http.delete<boolean>(`/api/workflow/delete/${id}`);
};

// 批量删除工作流
export const deleteWorkflows = (ids: number[]) => {
  return http.delete<boolean>('/api/workflow/delete/batch', { data: ids });
};

// 获取工作流详情
export const getWorkflowDetail = (id: number) => {
  return http.get<Workflow>(`/api/workflow/detail/${id}`);
};

// 发布工作流
export const publishWorkflow = (id: number) => {
  return http.put<boolean>(`/api/workflow/publish/${id}`);
};

// 停用工作流
export const stopWorkflow = (id: number) => {
  return http.put<boolean>(`/api/workflow/stop/${id}`);
};

// 获取工作流图数据
export const getWorkflowGraphData = (id: number) => {
  return http.get<WorkflowGraphData>(`/api/workflow/graph/${id}`);
};

// 保存工作流图数据
export const saveWorkflowGraphData = (id: number, graphData: WorkflowGraphData) => {
  return http.put<boolean>(`/api/workflow/graph/${id}`, graphData);
};

// 获取工作流实例列表
export const getWorkflowInstanceList = (params: any) => {
  return http.post<PageResult<WorkflowInstance>>('/api/workflow/instance/list', params);
};

// 启动工作流实例
export const startWorkflowInstance = (workflowId: number, businessKey: string, variables?: Record<string, any>) => {
  return http.post<number>('/api/workflow/instance/start', {
    workflowId,
    businessKey,
    variables
  });
};

// 获取工作流实例详情
export const getWorkflowInstanceDetail = (id: number) => {
  return http.get<WorkflowInstance>(`/api/workflow/instance/detail/${id}`);
};

// 获取工作流轨迹
export const getWorkflowTrace = (instanceId: number) => {
  return http.get<WorkflowTrace[]>(`/api/workflow/trace/${instanceId}`);
};

// 完成任务
export const completeTask = (taskId: string, variables?: Record<string, any>, comment?: string) => {
  return http.post<boolean>('/api/workflow/task/complete', {
    taskId,
    variables,
    comment
  });
};

// 拒绝任务
export const rejectTask = (taskId: string, comment?: string) => {
  return http.post<boolean>('/api/workflow/task/reject', {
    taskId,
    comment
  });
};

// 暂停工作流实例
export const pauseWorkflowInstance = (instanceId: number) => {
  return http.put<boolean>(`/api/workflow/instance/pause/${instanceId}`);
};

// 恢复工作流实例
export const resumeWorkflowInstance = (instanceId: number) => {
  return http.put<boolean>(`/api/workflow/instance/resume/${instanceId}`);
};

// 终止工作流实例
export const terminateWorkflowInstance = (instanceId: number, reason?: string) => {
  return http.put<boolean>(`/api/workflow/instance/terminate/${instanceId}`, { reason });
};

